﻿namespace LS.Nc6.Application
{
    public abstract class BaseService<T> : IBaseInterface<T> where T : class, new()
    {
        public Task<int> DeleteAsync(string ids, int configId = 0)
        {
            var arryid = ids.GetStringToLongArryNoNull();
            if (arryid.Length > 0)
            {
               return  DbContext.Instance.Deleteable<T>().Where(" id in (@id) ", new { id = arryid }).IsLogic().ExecuteCommandAsync();
            }
            return  Task.FromResult(0);
        }

        public Task< T> GetByIdAsync(long id, int configId = 0)
        {
            return DbContext.Instance.Queryable<T>().InSingleAsync(id);
        }

        public Task<long> InsertAsync(T model, int configId = 0)
        {
            return DbContext.Instance.Insertable(model).ExecuteReturnBigIdentityAsync();
        }

        public Task<int> RemoveAsync(string ids, int configId = 0)
        {
            var arryid = ids.GetStringToLongArryNoNull();
            if (arryid.Length > 0)
            {
                return DbContext.Instance.Deleteable<T>().Where(" id in (@id) ", new { id = arryid }).ExecuteCommandAsync();
            }
            return Task.FromResult(0);
        }

        public Task<int> UpdateAsync(T model, Expression<Func<T, object>> columns, int configId = 0)
        {
            return DbContext.Instance.Updateable(model).IgnoreColumns(columns).ExecuteCommandAsync();
        }

        public Task<int> UpdateAsync(T model, int configId = 0)
        {
            return DbContext.Instance.Updateable(model).ExecuteCommandAsync();
        }
       
        public Task<SqlSugarPagedList<T>> PageList(Expression<Func<T, bool>> expression, int page, int limit, Expression<Func<T, object>> expressionOrder)
        {
            return DbContext.Instance.Queryable<T>().Where(expression).OrderByDescending(expressionOrder).ToPagedListAsync(page, limit);
        }
        public   async Task<Tuple<List<dynamic>,int,int>> PageDynamicList(Expression<Func<T, bool>> expression, int page, int limit, bool desc, Expression<Func<T, object>> expressionOrder, Expression<Func<T, dynamic>> expressionSelect)
        {
            RefAsync<int> totalCount = 0;
            List<dynamic> items = null;
            if (desc)
            {
                 items = await DbContext.Instance.Queryable<T>().Where(expression).OrderByDescending(expressionOrder).Select(expressionSelect).ToPageListAsync(page, limit, totalCount);
            }
            else
            {
                items = await DbContext.Instance.Queryable<T>().Where(expression).OrderBy(expressionOrder).Select(expressionSelect).ToPageListAsync(page, limit, totalCount);
            }
            var totalPages = (int)Math.Ceiling(totalCount / (double)limit);
            return new Tuple<List<dynamic>, int, int>(
                 items,(int)totalCount,totalPages
            );
        }
        public Task<List<T>> List(Expression<Func<T, bool>> expression, Expression<Func<T, object>> expressionOrder)
        {
            return DbContext.Instance.Queryable<T>().Where(expression).OrderBy(expressionOrder).ToListAsync();
        }
        public Task<List<T>> List(Expression<Func<T, bool>> expression, Expression<Func<T, object>> expressionOrder, Expression<Func<T, dynamic>> expressionSelect)
        {
            return DbContext.Instance.Queryable<T>().Where(expression).OrderBy(expressionOrder).ToListAsync();
        }

    }
}
